package code.google.gui.fragments.player;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import code.google.controller.interfaces.IPlayerController;
import code.google.controller.player.PlayerController;
import code.google.gui.fragments.RegistrateableFragment;
import code.google.gui.fragments.interfaces.IPlayerDispenser;
import code.google.gui.fragments.interfaces.IPlayerListener;
import code.google.gui.interfaces.IPlayerFragment;
import code.google.model.util.interfaces.ISong;

import com.example.metamusicgui.R;

public class PlayerFragment extends RegistrateableFragment implements IPlayerFragment, IPlayerListener/*implements  SeekBar.OnSeekBarChangeListener*/ {

	private ImageButton btnPlay;
	private ImageButton btnForward;
	private ImageButton btnBackward;
	private ImageButton btnNext;
	private ImageButton btnPrevious;
//	private ImageButton btnPlaylist;
	private ImageButton btnRepeat;
	private ImageButton btnShuffle;
	private SeekBar songProgressBar;
	private TextView songTitleLabel;
	private TextView songCurrentDurationLabel;
	private TextView songTotalDurationLabel;
	
	private IPlayerController controller;
	private boolean isAlreadyPlaying = false;
	
	

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.player, container, false);
		
		
		
//		songProgressBar.setOnSeekBarChangeListener(this); // Important
		
		// All player buttons
		btnPlay = (ImageButton) rootView.findViewById(R.id.btnPlay);
		btnForward = (ImageButton) rootView.findViewById(R.id.btnForward);
		btnBackward = (ImageButton) rootView.findViewById(R.id.btnBackward);
		btnNext = (ImageButton) rootView.findViewById(R.id.btnNext);
		btnPrevious = (ImageButton) rootView.findViewById(R.id.btnPrevious);
//		btnPlaylist = (ImageButton) rootView.findViewById(R.id.btnPlaylist);
		btnRepeat = (ImageButton) rootView.findViewById(R.id.btnRepeat);
		btnShuffle = (ImageButton) rootView.findViewById(R.id.btnShuffle);
		songProgressBar = (SeekBar) rootView.findViewById(R.id.songProgressBar);
		songTitleLabel = (TextView) rootView.findViewById(R.id.songTitle);
		songCurrentDurationLabel = (TextView) rootView.findViewById(R.id.songCurrentDurationLabel);
		songTotalDurationLabel = (TextView) rootView.findViewById(R.id.songTotalDurationLabel);
		
		
		
		/**
		 * Play button click event
		 * plays a song and changes button to pause image
		 * pauses a song and changes button to play image
		 * */
		btnPlay.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if(controller.isPlaying()){
					controller.pause();
				}
				else{
					controller.play();
				}
			}
		});
		
		setPlayingButton(isAlreadyPlaying);
		
		/**
		 * Forward button click event
		 * Forwards song specified seconds
		 * */
		btnForward.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				
			}
		});
		
		/**
		 * Backward button click event
		 * Backward song to specified seconds
		 * */
		btnBackward.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				
				
			}
		});
		
		/**
		 * Next button click event
		 * Plays next song by taking currentSongIndex + 1
		 * */
		btnNext.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				controller.forward();
			}
		});
		
		/**
		 * Back button click event
		 * Plays previous song by currentSongIndex - 1
		 * */
		btnPrevious.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				controller.backward();
			}
		});
		
		/**
		 * Button Click event for Repeat button
		 * Enables repeat flag to true
		 * */
		btnRepeat.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				controller.looping();
			}
		});
		
		setRepeatingButton(controller.isRepeating());
		
		/**
		 * Button Click event for Shuffle button
		 * Enables shuffle flag to true
		 * */
		btnShuffle.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
//				if(isShuffle){
//					isShuffle = false;
////					Toast.makeText(getApplicationContext(), "Shuffle is OFF", Toast.LENGTH_SHORT).show();
//					btnShuffle.setImageResource(R.drawable.btn_shuffle);
//				}else{
//					// make repeat to true
//					isShuffle= true;
////					Toast.makeText(getApplicationContext(), "Shuffle is ON", Toast.LENGTH_SHORT).show();
//					// make shuffle to false
//					isRepeat = false;
//					btnShuffle.setImageResource(R.drawable.btn_shuffle_focused);
//					btnRepeat.setImageResource(R.drawable.btn_repeat);
//				}	
			}
		});
		
		/**
		 * Button Click event for Play list click event
		 * Launches list activity which displays list of songs
		 * */
//		btnPlaylist.setOnClickListener(new View.OnClickListener() {
//			
//			@Override
//			public void onClick(View arg0) {
//				Intent i = new Intent(getApplicationContext(), PlayListActivity.class);
//				startActivityForResult(i, 100);			
//			}
//		});
		
		return rootView;
		
	}
	
//	@Override
//	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
//		
//	}
	

	@Override
	public void registrateFragmentAt(Activity context) {
		if(context instanceof IPlayerDispenser){
			IPlayerDispenser dispenser = (IPlayerDispenser)context;
			dispenser.addPlayerListener(this);
		}
		controller = new PlayerController(this);
	}

	@Override
	public void playSong(ISong song) {
		controller.play(song);
	}

	@Override
	public void deregistrateFragmentFrom(Activity context) {
		if(context instanceof IPlayerDispenser){
			IPlayerDispenser dispenser = (IPlayerDispenser)context;
			dispenser.removePlayerListener(this);
		}
	}

	@Override
	public void setRepeatingButton(boolean isRepeat) {
		if(btnRepeat != null){
			if(!isRepeat){
				btnRepeat.setImageResource(R.drawable.btn_repeat);
			}else{
				btnRepeat.setImageResource(R.drawable.btn_repeat_focused);
			}	
		}
	}

	@Override
	public void setPlayingButton(boolean playing) {
		if(btnPlay != null){
			if(playing){
				btnPlay.setImageResource(R.drawable.btn_pause);
			}
			else{
				btnPlay.setImageResource(R.drawable.btn_play);
			}
		}
		else{
			isAlreadyPlaying = playing;
		}
	}
}